package com.example.web.others.runner;

import com.example.common.constant.SystemConstant;
import com.example.common.redis.RedisUtils;
import com.example.system.service.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.stream.Stream;

@Service
public class RunnerInit {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private MenuService menuService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private DictService dictService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ProcessService processService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private PermService permService;

    @Async
    @SneakyThrows
    @PostConstruct
    public void init() {
        System.out.println("===============删除本地缓存===============");
        redisUtils.delAll();
        System.out.println("===============删除本地文件===============");
        deleteFolder(SystemConstant.srppms);
        System.out.println("===============初始化权限===============");
        long time1 = System.currentTimeMillis();
        permService.createPermRedis();
        System.out.println("===============检查数据库表===============");
        long time2 = System.currentTimeMillis();
        commonService.checkDataBase();
        System.out.println("===============初始化父菜单===============");
        long time3 = System.currentTimeMillis();
        menuService.createMenuParentRedis();
        System.out.println("===============初始化菜单=================");
        long time4 = System.currentTimeMillis();
        menuService.createMenuRedis();
        System.out.println("===============初始化项目=================");
        long time5 = System.currentTimeMillis();
        itemService.createItemRedis();
        System.out.println("===============初始化角色=================");
        long time6 = System.currentTimeMillis();
        roleService.createRoleRedis();
        System.out.println("===============初始化通知=================");
        long time7 = System.currentTimeMillis();
        noticeService.createNoticeRedis();
        System.out.println("===============初始化审核=================");
        long time8 = System.currentTimeMillis();
        processService.createShareUserRedis();
        System.out.println("===============初始化字典=================");
        long time9 = System.currentTimeMillis();
        dictService.createDictRedis();
        long time10 = System.currentTimeMillis();
        System.out.println("==============各项耗时如下=================");
        System.out.printf("\t\t\t初始化权限:%d ms\n", time2 - time1);
        System.out.printf("\t\t\t检查数据库:%d ms\n", time3 - time2);
        System.out.printf("\t\t\t初始父菜单:%d ms\n", time4 - time3);
        System.out.printf("\t\t\t初始化菜单:%d ms\n", time5 - time4);
        System.out.printf("\t\t\t初始化项目:%d ms\n", time6 - time5);
        System.out.printf("\t\t\t初始化角色:%d ms\n", time7 - time6);
        System.out.printf("\t\t\t初始化通知:%d ms\n", time8 - time7);
        System.out.printf("\t\t\t初始化审核:%d ms\n", time9 - time8);
        System.out.printf("\t\t\t初始化字典:%d ms\n", time10 - time9);
        System.out.println("===============初始化完成=================");
    }

    /**
     * 删除文件夹以及所有文件
     *
     * @param path 地址
     */
    public static void deleteFolder(String path) {
        Path directory = Paths.get(path);
        try (Stream<Path> stream = Files.walk(directory).sorted(Comparator.reverseOrder())) {
            stream.forEach(o -> {
                try {
                    Files.delete(o);
                } catch (IOException ignored) {
                }
            });
        } catch (IOException ignored) {
        }
    }
}
